package common.utils.net;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class HttpClientUtils {
	
	private static HttpClientUtils netWorkUtils = new HttpClientUtils();
	
	private static HttpClient httpclient = null;
	
	public static HttpClientUtils getInstance(){
		return netWorkUtils;
	}
	
	/**
	 * 检查网络是否可用
	 * @param context
	 * @return
	 */
	public boolean checkNetState(Context context){
		boolean isConnection = false;
		
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNet = connectivityManager.getActiveNetworkInfo();
//		NetworkInfo mobileNet = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//		NetworkInfo wifiNet = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if(activeNet != null){
			isConnection = true;
		}
		return isConnection;
	}
	
	/**
	 * 发送xml文件
	 * @param file
	 * @param url
	 * @return
	 */
	public synchronized boolean sendFileData(File file, String url) {
		httpclient = new DefaultHttpClient();
		boolean isSended = false;
		HttpResponse response = null;
		httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httppost = new HttpPost(url);
		FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
		httppost.setEntity(reqEntity);
		reqEntity.setContentType("binary/octet-stream");
		try {
			response = httpclient.execute(httppost);
			HttpEntity resEntity = null;
			if(response.getStatusLine().getStatusCode()<400){
				resEntity = response.getEntity();
				if (resEntity != null) {
					
				}
			}
			if (resEntity != null) {
				resEntity.consumeContent();
			}
		} catch (ClientProtocolException e) {
			Log.e("System.out", e.getMessage());
		} catch (IOException e) {
			Log.e("System.out", e.getMessage());
		}
		httpclient.getConnectionManager().shutdown();
		return isSended;
	}
	
	
	public synchronized String getNetString(String url){
		httpclient = new DefaultHttpClient();
		String s = null;
		HttpResponse response = null;
		HttpGet httpGet = new HttpGet(url);
		try {
			response = httpclient.execute(httpGet);
			HttpEntity resEntity = null;
			if(response.getStatusLine().getStatusCode()<400){
				resEntity = response.getEntity();
				s = entityToString(resEntity.getContent());
			}
			if (resEntity != null) {
				resEntity.consumeContent();
			}
		} catch (ClientProtocolException e) {
			Log.e("System.out", e.getMessage());
		} catch (IOException e) {
			Log.e("System.out", e.getMessage());
		}
		httpclient.getConnectionManager().shutdown();
		return s;
	}
	
	private String entityToString(InputStream inputStream){
		StringBuffer sb = new StringBuffer();
		try {
			int i = 0;
			byte[] b = new byte[1024];
			
			while((i=inputStream.read(b)) != -1){
				sb.append(new String(b, 0, i));
			}
		} catch (IOException e) {
			Log.e("System.out", e.getMessage());
		}
		return sb.toString().trim();
	}
	

}
